#include <iostream>

#include <gtest/gtest.h>

#include "melon/client/thrift_client_pool.h"
#include "bbt/adapters/bbt_engineer_message_outlet_adapter.h"

using bbt::BbtEngineerMessageOutletAdapter;
using melon::client::ClientPool;

using namespace bbt;

void * ThreadFun(void *) {
  for (int i = 0; i < 10000; ++i) {
    BbtMessage msg;
    int ret = BbtEngineerMessageOutletAdapter::Instance().PushMessage(msg, 2);
    std::cout << "PushMessage " << i << " ret=" << ret << std::endl;

  //ImMessage message;
  //message.sender = 127646882 + i;
  //message.receiver = 127646882;
  //message.body = "bang_robot_test_message";
  //ret = BangRobotAdapter::Instance().DispatchMessage(message, 2);
  //std::cout << "DispatchMessage " << i << " ret=" << ret << std::endl;
  //
      //BbtEngineerAccessServiceClient * client = pool_.Alloc(GetServerKey());
      //if (client == NULL) {
      //  LOG_WARN("BbtEngineerAccessServiceClient connect err, sender=" << message.sender << " receiver=" << message.receiver);
      //  return -1;
      //}

      //bool success = true;
      //try {
      //  client->PutMessage(message);
      //  LOG_INFO("BbtEngineerAccessServiceClient CreateMessage success. sender=" << message.sender << " receiver=" << message.receiver);
      //} catch (const std::exception & e) {
      //  LOG_WARN("BbtEngineerAccessServiceClient CreateMessage error. sender=" << message.sender << " receiver=" << message.receiver
      //            << " e=" << e.what());
      //  success = false;
      //}
      //pool_.Release(GetServerKey(), client, success);
      //if (success) {
      //  return 0;
      //}
  }
  return NULL;
}

const int kThreadCount = 20;

int main() {
  LOG_INIT("./test.log", "DEBUG");
  pthread_t ts[kThreadCount];

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, NULL);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
} 
